import {createRouter,createWebHashHistory} from 'vue-router'


import login from '../components/login.vue'
import Regist from '../components/Regist.vue'
import ShowSchedule from '../components/ShowSchedule.vue'
import Login from '../components/login.vue'
import pinia from "../pinia.js"
import {defineUser} from "../store/userStore.js"

let sysUser=defineUser(pinia)

const router=createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            redirect:"/showSchedule"
        },
        {
            path:"/ShowSchedule",
            component:ShowSchedule
        },
        {
            path:"/login",
            component:Login
        },
        {
            path:"/regist",
            component:Regist
        }
    ]
})

//路由的全局守卫  判断是否可以访问showschedule
router.beforeEach((to,from,next)=>{
    if(to.path=="/showSchedule"){
        //登录了就放行，没有登陆就阻断
        if(sysUser.username==''){
            next('/login')
        }else{
            next()
        }
    }else{
        next()
    }
})

export default router